class_name PlayerStats
extends Resource

## 升级所需经验值
const EXP_REQUIREMENTS := {
	1: 0,
	2: 2,
	3: 2,
	4: 6,
	5: 10,
	6: 20,
	7: 36,
	8: 48,
	9: 76,
	10: 76
}

## 每个等级下可以刷新出来的稀有度
const ROLL_RARITIES := {
	1:  [UnitStats.Rarity.COMMON],
	2:  [UnitStats.Rarity.COMMON],
	3:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON],
	4:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE],
	5:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE],
	6:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE],
	7:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.LEGENDARY],
	8:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.LEGENDARY],
	9:  [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.LEGENDARY],
	10: [UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.LEGENDARY]
}

## 每个等级下刷新出来的稀有度的概率
const ROLL_CHANCES := {
	1: 	[1],
	2: 	[1],
	3: 	[7.5, 2.5],
	4: 	[6.5, 3.0, 0.5],
	5: 	[5.0, 3.5, 1.5],
	6: 	[4.0, 4.0, 2.0],
	7: 	[2.75, 4.0, 3.24, 0.1],
	8: 	[2.5, 3.75, 3.45, 0.3],
	9: 	[1.75, 2.75, 4.5, 1.0],
	10: [1.0, 2.0, 4.5, 2.5],
}

## 玩家的金币数量
@export_range(0, 99) var gold: int : set = _set_gold

## 玩家的经验值
@export_range(0, 99) var experience: int : set = _set_exp

## 玩家的等级
@export_range(1, 10) var level: int : set = _set_level


## 获取当前等级下的随机稀有度
func get_random_rarity_for_level() -> UnitStats.Rarity:
	var rng = RandomNumberGenerator.new()
	var array: Array = ROLL_RARITIES[level]
	var weights: PackedFloat32Array = PackedFloat32Array(ROLL_CHANCES[level])

	return array[rng.rand_weighted(weights)]


## 获取当前等级下的升级所需经验值
func get_current_exp_requirement() -> int:
	var next_level = clampi(level + 1, 1, 10)
	return EXP_REQUIREMENTS[next_level]


func _set_gold(value: int) -> void:
	gold = value
	emit_changed()


func _set_exp(value: int) -> void:
	experience = value
	emit_changed()
	
	if level == 10:
		return
	
	var exp_requirement: int = get_current_exp_requirement()
	
	while level < 10 and experience >= exp_requirement:
		level += 1
		experience -= exp_requirement
		exp_requirement = get_current_exp_requirement()
		emit_changed()


func _set_level(value: int) -> void:
	level = value
	emit_changed()
